// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#ifndef MGCBEZIERCYLINDER2_H
#define MGCBEZIERCYLINDER2_H

#include "MgcBezierCylinder.h"
#include "MgcColor.h"
#include "MgcVector2.h"
#include "MgcVector3.h"


class MgcBezierCylinder2 : public MgcBezierCylinder
{
    MgcDeclareRTTI;
    MgcDeclareStream;

public:
    // Construction.  MgcBezierCylinder2 accepts responsibility for deleting
    // the input array.
    MgcBezierCylinder2 (unsigned int* auiIndex);

    // tessellation
    virtual void Tessellate (unsigned int uiLevel,
        const MgcVector3* akCtrlPoint, const MgcColor* akCtrlColor,
        const MgcVector2* akCtrlTexture, MgcTriMesh* pkMesh,
        unsigned int& ruiVertexStart, unsigned int& ruiTriangleStart);

protected:
    MgcBezierCylinder2 ();

    // precomputation
    class IntervalParameters
    {
    public:
        // s = curve direction, t = cylinder direction
        unsigned int m_uiI00, m_uiI01, m_uiI10, m_uiI11;
        MgcVector3 m_aakXss[2];
        MgcColor m_aakCss[2];
        MgcVector2 m_aakTss[2];
    };

    void InitializePoints (const MgcVector3* akCtrlPoint,
        IntervalParameters& rkIP);

    void InitializeColors (const MgcColor* akCtrlColor,
        IntervalParameters& rkIP);

    void InitializeTextures (const MgcVector2* akCtrlTexture,
        IntervalParameters& rkIP);

    // tessellate cylinder curves by recursive subdivision
    void SubdivideCylinder (unsigned int uiCLevel, MgcVector3* akX,
        MgcVector3* akXs, MgcVector3* akXt, MgcColor* akColor,
        MgcVector2* akTexture, unsigned int uiI0, unsigned int uiI1,
        unsigned int uiTwoPowL);

    // tessellate boundary curves by recursive subdivision
    void SubdivideBoundary (unsigned int uiLevel, MgcReal fDSqr,
        MgcVector3* akX, MgcVector3* akXs, MgcVector3* akXt,
        MgcColor* akColor, MgcVector2* akTexture, IntervalParameters& rkIP);
};

MgcSmartPointer(MgcBezierCylinder2);
MgcRegisterStream(MgcBezierCylinder2);

#endif
